using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Common
{
    /// <summary>
    /// 
    /// </summary>
    public class FileUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="appName"></param>
        /// <returns></returns>
        public static  string GetWindowsAppFolderPath(string appName)
        {
            string generalDataFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string appDataFolderPath = Path.Combine(generalDataFolderPath, appName);
            if(!Directory.Exists(appDataFolderPath))
            {
                Directory.CreateDirectory(appDataFolderPath);
            }
            return appDataFolderPath;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string FormatFileName(string fileName)
        {
            char[] escapedChars = new char[]{
                '!','@','#','$','%','^','&','*','+','=',
                '~','`','|','\\','/','?',',',':',';','\'','\"'};
            for (int i = 0; i < escapedChars.Length; i++)
            {
                fileName = fileName.Replace(escapedChars[i], '_');
            }
            return fileName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="appName"></param>
        /// <param name="extension"></param>
        /// <returns></returns>
        public static string GetWindowsAppTempFilePath(string appName, string extension)
        {
            string appFolderPath=FileUtil.GetWindowsAppFolderPath(appName);
            string tmpFolderPath = appFolderPath + "\\temp";
            if (!System.IO.Directory.Exists(tmpFolderPath))
            {
                System.IO.Directory.CreateDirectory(tmpFolderPath);
            }
            string tmpFileName = Guid.NewGuid().ToString() + extension;
            string tmpFilePath = System.IO.Path.Combine(tmpFolderPath, tmpFileName);
            return tmpFilePath;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="appName"></param>
        /// <param name="expectedFileName"></param>
        /// <param name="extension"></param>
        /// <returns></returns>
        public static string CreateNewWindowsAppTempFilePath(string appName, string expectedFileName, string extension)
        {
            string appFolderPath = FileUtil.GetWindowsAppFolderPath(appName);
            string tmpFolderPath = appFolderPath + "\\temp";
            if (!System.IO.Directory.Exists(tmpFolderPath))
            {
                System.IO.Directory.CreateDirectory(tmpFolderPath);
            }
            string tmpFileName = expectedFileName + extension;
            int trialNum = 0;
            string tmpFilePath = System.IO.Path.Combine(tmpFolderPath, tmpFileName);
            while(File.Exists(tmpFilePath))
            {
                trialNum += 1;
                tmpFileName = expectedFileName + "_" + trialNum + extension;
                tmpFilePath = Path.Combine(tmpFolderPath, tmpFileName);
            }
            return tmpFilePath;
        }
    }
}
